package com.cynteka.cyfoman.core.collection;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

/**
 * User: Sergey
 * Date: 29.07.11
 * Time: 12:34
 */
public class FileCollection extends BaseCollection<File> implements IFileCollection {
    private final File baseFolder;
    private boolean filterHiddenFiles = false;

    public boolean isFilterHiddenFiles() {
        return filterHiddenFiles;
    }

    public void setFilterHiddenFiles(boolean filterHiddenFiles) {
        this.filterHiddenFiles = filterHiddenFiles;
    }

    public FileCollection(String id, File baseFolder) {
        super(id);
        this.baseFolder = baseFolder;
    }

    @Override
    public Collection<File> getItems(Map<String, Object> params) {
        File parent = params == null ? null : (File) params.get(Constants.PARENT);
        if (parent == null)
            parent = baseFolder;

        FileFilter filter = new HiddenFileFilter(filterHiddenFiles);
        return Arrays.asList(parent.listFiles() == null ? new File[0] : parent.listFiles(filter));
    }

    public File getBaseFolder() {
        return baseFolder;
    }
}
